const app = getApp()
Page({
  data: {
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    category: [],
    worksList: null,
    load: true,
    login: null,
  },
  onLoad() {
    this.isLogin();
    this.query();
  },
  onShow: function () {
    wx.hideHomeButton()
  },
  isLogin() {
    const login = wx.getStorageSync('login')
    const token_expires = wx.getStorageSync('token_expires')
    if (login) {

      if (token_expires) {
        // 检测临时账户有效期
        const now = Number(new Date());
        if (token_expires > now) {
          // 临时账户在有效期内
          this.setData({
            login
          })
        } else {
          // 临时用户过期了
          wx.clearStorage({
            success: (res) => {
              wx.showToast({
                title: '登录过期',
                icon: 'none'
              }).then(res => {
                this.setData({
                  login: 0
                })
              })
            },
          })
        }
      } else {
        // 永久账户
        this.setData({
          login
        })
      }

    } else {
      // 用户没登录
      this.setData({
        login: 0
      })
    }

  },
  toDetail(e) {
    const data = JSON.stringify(e.currentTarget.dataset.detail)
    wx.navigateTo({
      url: `/pages/works/detail/detail?data=${data}`,
    })
  },

  query() {
    // 调用云函数
    wx.cloud.callFunction({
      name: 'getWorks',
      data: {},
      success: res => {
        console.log('[云函数] [getWorks]: ', res.result.data)
        const {
          category,
          worksList
        } = res.result.data
        // const works_list = worksList.map((work, index) => {
        //   const cur = category.find(item => {
        //     return item.id == (work[0] && work[0].parentId)
        //   })
        //   let id;
        //   if (cur) {
        //     id = cur.id
        //   }
        //   return {
        //     id: index,
        //     parentId: id,
        //     data: work,
        //   }
        // })
        this.setData({
          category,
          worksList,
          listCur: category[0]
        })
      },
      fail: err => {
        console.error('[云函数] [getWorks] 调用失败', err)
      }
    })
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    console.log(e)
    let that = this;
    let worksList = this.data.worksList;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < worksList.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + i);
        // let view = wx.createSelectorQuery().select("#main-" + worksList[i].id);
        console.log(view)
        view.fields({
          size: true
        }, data => {
          console.log(data)
          worksList[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          worksList[i].bottom = tabHeight;
        }).exec();
        console.log(view)
      }
      that.setData({
        load: false,
        worksList: worksList
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < worksList.length; i++) {
      if (scrollTop > worksList[i].top && scrollTop < worksList[i].bottom) {
        that.setData({
          VerticalNavTop: (worksList[i].id - 1) * 50,
          TabCur: worksList[i].id
        })
        return false
      }
    }
  }
})